<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="赛道名称" prop="trackName">
        <el-input
          v-model="queryParams.trackName"
          placeholder="请输入赛道名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名开始" prop="registrationStartTime">
        <el-date-picker clearable
          v-model="queryParams.registrationStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择报名开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名结束" prop="registrationEndTime">
        <el-date-picker clearable
          v-model="queryParams.registrationEndTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择报名结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="比赛开始" prop="competitionStartTime">
        <el-date-picker clearable
          v-model="queryParams.competitionStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择比赛开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="比赛结束" prop="competitionEndTime">
        <el-date-picker clearable
          v-model="queryParams.competitionEndTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择比赛结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="比赛进度" prop="competitionProgress">
        <el-input
          v-model="queryParams.competitionProgress"
          placeholder="报名中,备赛中,已结束"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="竞赛类别" prop="competitionCategory">
        <el-input
          v-model="queryParams.competitionCategory"
          placeholder="设计,算法,软件开发,创新创业"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:teamrecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:teamrecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:teamrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:teamrecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="赛道名称" align="center" prop="trackName" />
      <el-table-column label="人数限制" align="center" prop="personLimit" />
      <el-table-column label="报名开始" align="center" prop="registrationStartTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.registrationStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束" align="center" prop="registrationEndTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.registrationEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛开始" align="center" prop="competitionStartTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.competitionStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛结束" align="center" prop="competitionEndTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.competitionEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="竞赛负责人" align="center" prop="competitionLeader" />
      <el-table-column label="联系方式" align="center" prop="contactWay" />
      <el-table-column label="比赛进度" align="center" prop="competitionProgress" />
      <el-table-column label="竞赛类别" align="center" prop="competitionCategory" />
      <!-- <el-table-column label="是否禁用(0:否,1:是)" align="center" prop="isDisabled" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:teamrecord:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:teamrecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改团队赛记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="teamrecordRef" :model="form" :rules="rules" label-width="80px">
       <!--  <el-form-item label="关联竞赛表id" prop="competitionId">
          <el-input v-model="form.competitionId" placeholder="请输入关联竞赛表id" />
        </el-form-item> -->
        <el-form-item label="竞赛具体名称" prop="competitionName">
          <el-input v-model="form.competitionName" placeholder="请输入竞赛具体名称"  disabled />
        </el-form-item>
        <el-form-item label="赛道名称" prop="trackName">
          <el-input v-model="form.trackName" placeholder="请输入赛道名称" />
        </el-form-item>
        <el-form-item label="赛道描述" prop="trackDescription">
          <el-input v-model="form.trackDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="人数限制" prop="personLimit">
          <el-input v-model="form.personLimit" placeholder="请输入人数限制" />
        </el-form-item>
        <el-form-item label="报名开始" prop="registrationStartTime">
          <el-date-picker clearable
            v-model="form.registrationStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束" prop="registrationEndTime">
          <el-date-picker clearable
            v-model="form.registrationEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛开始" prop="competitionStartTime">
          <el-date-picker clearable
            v-model="form.competitionStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择比赛开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛结束" prop="competitionEndTime">
          <el-date-picker clearable
            v-model="form.competitionEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择比赛结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="竞赛负责人" prop="competitionLeader">
          <el-input v-model="form.competitionLeader" placeholder="请输入竞赛负责人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactWay">
          <el-input v-model="form.contactWay" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="比赛进度" prop="competitionProgress">
          <el-input v-model="form.competitionProgress" placeholder="报名中,备赛中,已结束" />
        </el-form-item>
        <el-form-item label="竞赛类别" prop="competitionCategory">
          <el-input v-model="form.competitionCategory" placeholder="设计,算法,软件开发,创新创业" />
        </el-form-item>
        <el-form-item label="是否禁用" prop="isDisabled">
          <el-input v-model="form.isDisabled" placeholder="请输入是否禁用(0:否,1:是)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Teamrecord">
import { listTeamrecord, getTeamrecord, delTeamrecord, addTeamrecord, updateTeamrecord } from "@/api/competitionmanagement/teamrecord";

// 定义props接收父组件传递的竞赛信息
const props = defineProps({
  competitionId: {
    type: String,
    default: ''
  },
  competitionName: {
    type: String,
    default: ''
  }
});

const { proxy } = getCurrentInstance();

const teamrecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    competitionId: props.competitionId,
    competitionName: null,
    trackName: null,
    trackDescription: null,
    personLimit: null,
    registrationStartTime: null,
    registrationEndTime: null,
    competitionStartTime: null,
    competitionEndTime: null,
    competitionLeader: null,
    contactWay: null,
    competitionProgress: null,
    competitionCategory: null,
    isDisabled: null,
  },
  rules: {
    competitionId: [
      { required: true, message: "关联竞赛表id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询团队赛记录列表 */
function getList() {
  loading.value = true;
  listTeamrecord(queryParams.value).then(response => {
    teamrecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    competitionId: null,
    competitionName: null,
    trackName: null,
    trackDescription: null,
    personLimit: null,
    registrationStartTime: null,
    registrationEndTime: null,
    competitionStartTime: null,
    competitionEndTime: null,
    competitionLeader: null,
    contactWay: null,
    competitionProgress: null,
    competitionCategory: null,
    isDisabled: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("teamrecordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  // 自动填充竞赛ID和名称
  form.value.competitionId = props.competitionId;
  form.value.competitionName = props.competitionName;
  open.value = true;
  title.value = "添加团队赛记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTeamrecord(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改团队赛记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["teamrecordRef"].validate(valid => {
    if (valid) {
      proxy.$modal.loading("正在提交中...");
      if (form.value.id != null) {
        updateTeamrecord(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      } else {
        addTeamrecord(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除团队赛记录编号为"' + _ids + '"的数据项？').then(function() {
    return delTeamrecord(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/teamrecord/export', {
    ...queryParams.value
  }, `teamrecord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
